<?php
/*****************************************************************************
 **                                                                         ** 
 **                                         .o.                   o8o  	    **
 **                                        .888.                  `"'  	    **
 **     ooo. .oo.  .oo.   oooo    ooo     .8"888.     oo.ooooo.  oooo  	    **
 **     `888P"Y88bP"Y88b   `88.  .8'     .8' `888.     888' `88b `888  	    **
 **      888   888   888    `88..8'     .88ooo8888.    888   888  888  	    **
 **      888   888   888     `888'     .8'     `888.   888   888  888  	    **
 **     o888o o888o o888o     .8'     o88o     o8888o  888bod8P' o888o      **
 **                       .o..P'                       888             	    **
 **                       `Y8P'                       o888o            	    **
 **                                                                         **
 **                                                                         **
 **   Joomla! 1.5 Plugin myApiConnect                                       **
 **   @Copyright Copyright (C) 2010 - Thomas Welton                         **
 **   @license GNU/GPL http://www.gnu.org/copyleft/gpl.html                 **	
 **                                                                         **	
 **   myApiConnect is free software: you can redistribute it and/or modify  **
 **   it under the terms of the GNU General Public License as published by  **
 **   the Free Software Foundation, either version 3 of the License, or	    **	
 **   (at your option) any later version.                                   **
 **                                                                         **
 **   myApiConnect is distributed in the hope that it will be useful,	    **
 **   but WITHOUT ANY WARRANTY; without even the implied warranty of	    **
 **   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         **
 **   GNU General Public License for more details.                          **
 **                                                                         **
 **   You should have received a copy of the GNU General Public License	    **
 **   along with myApiConnect.  If not, see <http://www.gnu.org/licenses/>  **
 **                                                                         **			
 *****************************************************************************/
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.plugin.plugin');


class plgSystemmyApiConnect extends JPlugin
{
	
	function plgSystemmyApiConnect(&$subject, $config)
	{
		parent::__construct($subject, $config);
	}
	
	
	function onAfterInitialise()
	{
		
		if(!file_exists(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_myapi'.DS.'helpers'.DS.'facebook.php')){ return; }
		//This imports the Facebook php libary and creates a global obecjt for use accross the whole site.
		global $mainframe;
		
		 $doc = & JFactory::getDocument();
		
if($mainframe->isAdmin()) { 
	if((JRequest::getVar('view','','get') == 'settings') && (JRequest::getVar('option','','get') == 'com_myapi')){

		 $doc->addScript('http://connect.facebook.net/en_GB/all.js');
		 return;
	}else{
		return;
	}
}
		
		if($mainframe->isAdmin()) { return; } 
		
		$com_params = &JComponentHelper::getParams( 'com_myapi' );
		require_once JPATH_ADMINISTRATOR.DS.'components'.DS.'com_myapi'.DS.'helpers'.DS.'facebook.php';
		$GLOBALS['facebook'] =& new myApiFacebook(array(
			'appId'  => $com_params->get('appId'),
			'secret' => $com_params->get('secret'),
			'cookie' => true, // enable optional cookie support
		 ));
		
		//This initilises the javascript platform async
		
		jimport( 'joomla.language.helper' );
		  $lang = JLanguageHelper::detectLanguage();
		$js = <<<EOD
/* <![CDATA[ */
	
window.addEvent('domready',function(){
    FB.init({appId: "{$com_params->get('appId')}", status: false, cookie: true,
             xfbml: false});
	FB.XFBML.parse();
});
/* ]]> */
EOD;


		 JHTML::_('behavior.mootools');
	jimport( 'joomla.language.language' );
		  $lang = str_replace('-','_',JLanguageHelper::detectLanguage());
		 $doc->addScript('http://connect.facebook.net/'.$lang.'/all.js');
	


		 $doc->addScriptDeclaration($js);
	
	
	///This is the login on render feature
	
	}
	
	function onAfterRender()
	{
		if(!file_exists(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_myapi'.DS.'helpers'.DS.'facebook.php')){ return; }
		//For the async facebook injection
		global $mainframe;
		if($mainframe->isAdmin() && (JRequest::getVar('view','','get') == 'settings') && (JRequest::getVar('option','','get') == 'com_myapi')) { 
		
		jimport( 'joomla.environment.response' );
 		$buffer = JResponse::getBody();
		  $FeatureLoader_javascript = '<div id="fb-root"></div>';
		  $buffer = str_replace ("</body>", $FeatureLoader_javascript."</body>", $buffer);
		  $fbml	= '<html xmlns:fb="http://www.facebook.com/2008/fbml"';
		  $html	= str_replace( '<html' , $fbml , $buffer );
		 $fbml	= '<html xmlns:fb="http://www.facebook.com/2008/fbml"';
		  $html	= str_replace( '<html' , $fbml , $buffer );
		JResponse::setBody( $html );
		return true;
		}elseif($mainframe->isAdmin()){ return; } //Do not add from admin section
			
			$buffer = JResponse::getBody();
		  $FeatureLoader_javascript = '<div id="fb-root"></div><!-- Correct fb_xd_fragment Bug Start --> <script> document.getElementsByTagName("html")[0].style.display="block";</script> <!-- Correct fb_xd_fragment Bug End -->';
		  $buffer = str_replace ("</body>", $FeatureLoader_javascript."</body>", $buffer);
		  $fbml	= '<html xmlns:fb="http://www.facebook.com/2008/fbml"';
		  $html	= str_replace( '<html' , $fbml , $buffer );
			
		JResponse::setBody( $html );
		return true;
	}
	
	
	
	
}
?>